package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.ffcs.oss.common.utils.LocalMessageUtil;
import com.example.demo.enums.ExportListEnum;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.service.ExcelService;
import com.example.demo.service.NoticeService;
import com.example.demo.web.rest.constant.ExportConstant;
import com.example.demo.web.rest.evt.NoticeByIdEvt;
import com.example.demo.web.rest.evt.NoticeSaveEvt;
import com.example.demo.web.rest.evt.NoticeSearchEvt;
import com.example.demo.web.rest.vm.NoticeCodeList;
import com.example.demo.web.rest.vm.NoticeVm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiModel(description ="公告管理接口")
@RestController
@RequestMapping(value = "/api/notice")
public class NoticeController {

    protected Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ExcelService excelService;

    @ApiOperation("公告管理字典值")
    @PostMapping(value = "/getCodeList")
    public ServiceResp<NoticeCodeList> getCodeList() {
        return ServiceResp.getInstance().success(noticeService.getCodeList());
    }

    @ApiOperation("查询公告信息-总数")
    @PostMapping(value = "/selectInfosCount")
    public ServiceResp<Map<String, Object>> selectInfosCount(@RequestBody NoticeSearchEvt evt){
        Map<String, Object> map = new HashMap<>();
        map.put("total",noticeService.selectInfosCount(evt));
        return ServiceResp.getInstance().success(map);
    }

    @ApiOperation("查询公告信息-分页")
    @PostMapping(value = "/selectInfos")
    public ServiceResp<List<NoticeVm>> selectInfos(@RequestBody NoticeSearchEvt evt){
        List<NoticeVm> list = noticeService.selectInfos(evt);
        return ServiceResp.getInstance().success(list);
    }

    @ApiOperation("公告信息")
    @PostMapping(value = "/selectInfoById")
    public ServiceResp<NoticeVm> selectInfoById(@RequestBody NoticeByIdEvt evt){
        return ServiceResp.getInstance().success(noticeService.selectInfoById(evt));
    }

    @ApiOperation("新增-编辑公告信息")
    @PermissionAuth(code = "OSS-SETTING-NOTICE-ADD,OSS-SETTING-NOTICE-EDIT")
    @PostMapping(value = "/saveNoticeInfo")
    public ServiceResp saveNoticeInfo(@RequestBody NoticeSaveEvt evt){
        try{
            noticeService.saveInfos(evt);
            return ServiceResp.getInstance().success(true);
        } catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    @ApiOperation("发布公告信息")
    @PermissionAuth(code = "OSS-SETTING-NOTICE-EDIT")
    @PostMapping(value = "/publishNotice")
    public ServiceResp publishNotice(@RequestBody NoticeByIdEvt evt){
        try{
            noticeService.publishNotice(evt);
            return ServiceResp.getInstance().success(true);
        } catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    @ApiOperation("删除公告信息")
    @PermissionAuth(code = "OSS-SETTING-NOTICE-DEL")
    @PostMapping(value = "/deleteInfoById")
    public ServiceResp deleteInfoById(@RequestBody NoticeByIdEvt evt){
        try{
            noticeService.deleteInfoById(evt);
            return ServiceResp.getInstance().success(true);
        } catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    @ApiOperation("导出公告列表")
    @PermissionAuth(code = "OSS-SETTING-NOTICE-EXPORT")
    @GetMapping("/exportNotice")
    public void exportUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr) throws Exception {
        try{
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_NOTICE_MANAGE) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.NOTICE.getExpColEn(),ExportListEnum.NOTICE.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_NOTICE_MANAGE),ExportListEnum.NOTICE.getValue(),paramStr);
        }catch (Exception e) {
            logger.error(e.getMessage());
        }
    }


    @ApiOperation("获取最新公告")
    @PostMapping(value = "/getLatestNotice")
    public ServiceResp<List<NoticeVm>> getLatestNotice(@RequestBody int limitNum){
        return ServiceResp.getInstance().success(noticeService.getLatestNotice(limitNum));
    }
}
